<template>
  <div class="app-container">
    <!--    表单查询区-->
    <div class="filter-container">
      <el-input v-model="listQuery.key" placeholder="用户名或真实姓名" style="width: 200px; margin-right: 10px" class="filter-item" clearable @keyup.enter.native="getUserPage" @clear="getUserPage" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getUserPage">
        搜索
      </el-button>
      <!--<el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">-->
      <!--  添加-->
      <!--</el-button>-->
      <el-button v-waves :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">
        批量导出
      </el-button>
    </div>

    <!--表格区-->
    <el-table
      v-loading="listLoading"
      :data="list"
      row-key="id"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" prop="id" align="center" width="80" />
      <el-table-column label="用户名" prop="username" align="center" />
      <el-table-column label="真实姓名" prop="realName" align="center" />
      <el-table-column label="手机号" prop="phone" align="center" width="150px" />
      <el-table-column label="邮箱" prop="email" align="center" />
      <el-table-column label="角色" prop="roleName" align="center" width="150px" />
      <el-table-column label="账户状态" align="center" width="150px">
        <template v-slot="{row}">
          <el-switch
            v-model="row.state"
            style="display: block"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="激活"
            inactive-text="锁定"
            :active-value="0"
            :inactive-value="1"
            @change="handleState(row)"
          />
        </template>
      </el-table-column>
      <!--<el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">-->
      <!--  <template v-slot="{row}">-->
      <!--    <el-button type="primary" size="mini" @click="handleUpdate(row)">-->
      <!--      编辑-->
      <!--    </el-button>-->
      <!--    <el-button size="mini" type="danger" @click="handleDelete(row)">-->
      <!--      删除-->
      <!--    </el-button>-->
      <!--  </template>-->
      <!--</el-table-column>-->
    </el-table>

    <!--分页插件-->
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.current" :limit.sync="listQuery.size" @pagination="getUserPage" />

  </div>
</template>

<script>

import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination'
import { parseTime } from '@/utils'
import { getUserByList, getUserByPage, updateUserState } from '@/api/user'

export default {
  name: 'User',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      // 管理员列表
      list: [],
      // 总数
      total: 0,
      // 表格渲染标志
      listLoading: true,
      // 查询对象
      listQuery: {
        current: 1,
        size: 20,
        key: ''
      },
      downloadLoading: false
    }
  },
  created() {
    this.getUserPage()
  },
  methods: {
    parseTime,
    // 获取系统账户列表
    getUserPage() {
      getUserByPage(this.listQuery).then(res => {
        this.list = res.data.records
        this.total = res.data.total
        this.listLoading = false
      })
    },
    // 修改用户账户状态
    handleState(user) {
      updateUserState(user.id).then(res => {
        this.$notify.success({
          title: '成功',
          duration: 2000,
          message: res.msg
        })
      }).catch(() => {
        user.state = user.state === 0 ? 1 : 0
      })
    },
    // 下载
    handleDownload() {
      this.downloadLoading = true
      getUserByList().then(res => {
        import('@/vendor/Export2Excel').then(excel => {
          const tHeader = ['序号', '用户名', '手机号', '真实姓名', '角色']
          const filterVal = ['id', 'username', 'phone', 'realName', 'roleName']
          const data = this.formatJson(filterVal, res.data)
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: '系统账号列表' + parseTime(new Date(), '{y}-{m}-{d}-{h}-{i}-{s}')
          })
          this.downloadLoading = false
        })
      }).catch(() => {
        this.downloadLoading = false
        this.$notify.error({
          title: '失败',
          duration: 2000,
          message: '下载失败！'
        })
      })
    },
    formatJson(filterVal, list) {
      return list.map(v => filterVal.map(j => {
        return v[j]
      }))
    }
  }
}
</script>

<style scoped>

</style>
